import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * @modulename SourceCode
 * @description This module takes a text file and stores it line by line into a
 *              SourceCode class. This class is which contains the original line
 *              of source, a parsed version that will be used later and a
 *              version of the line that will be added to.
 * @callsequence SourceCode sourceCodeName = new SourceCode(filename);, where
 *               sourceCodeName is the name the user gives to the SourceCode
 *               object.
 * @inparam A text file that contains source code (filename)
 * @outparam creates a new SourceCode object
 * @errorstested tested through use with main
 * @errorsgenerated no known errors
 * @author Travis Casper
 * @createdate 10/12/09
 * @modlog <p>
 *         <ul>
 *         Who/When/Why Travis, 10/12/09, Added New Methods to Update
 *         Functionality
 *         </ul>
 *         Coding Standards Met: "Signed Code Leader(Jaron)"
 *         <p>
 *         Testing Standards Met: "Signed Test Leader(Jamie)"
 */
public final class SourceCode {
	private static String copyOfOriginal;
	private static ArrayList<String> sourceCodeDataObject;
	private static Integer currentLine;
	private static int originalCodeLength;

	public static void startSourceCode(File sourceFile) {
		sourceCodeDataObject = new ArrayList<String>();
		originalCodeLength = 0;
		currentLine = 0;
		copyOfOriginal = "";
		setUp(sourceFile);
	}
	
	private static final void setUp(File sourceFile) {
		String textInput = FileHandler.readFile(sourceFile);
		copyOfOriginal = textInput;
		Scanner in = new Scanner(textInput);
		String str;
		int counter = 0;
		//(str = in.nextLine()) != null
		while (in.hasNextLine()) {
			str = in.nextLine();
			// Import for Data Object
			sourceCodeDataObject.add(str);

			// Import for Original Copy
			// ***May have extra NewLine Character at the end***
			copyOfOriginal = copyOfOriginal + str + "\n";
			counter++;
		}
		System.out.println("ORIGINAL SOURCE CODE LENGTH" + counter);
		Main.appendDebugText("ORIGINAL SOURCE CODE LENGTH" + counter);
		originalCodeLength = counter;
		in.close();
	}
	
	/**
	 * @modulename returnNextLine
	 * @description Returns the next line of code and updates the current line
	 *              of code counter.
	 * @callsequence String = sourceCodeName.returnNextLine();, where
	 *               sourceCodeName is the name the user gives to the SourceCode
	 *               object.
	 * @inparam none
	 * @outparam An ArrayList of Type String that contains a line of Source Code
	 *           delimited by tabs. That is, every element of the ArrayList is a
	 *           Columnar element of SourceCode.
	 * @errorstested tested through use with main
	 * @errorsgenerated no known errors

	 * @createdate 10/12/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 * 
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
	 */
	public static final String returnNextLine() {
		String outputLine = "";
		outputLine = sourceCodeDataObject.get(currentLine);
		currentLine = currentLine + 1;
		return outputLine;
	}
	/**
	 * @modulename returnOriginalCodeLength
	 * @description Returns the number of original lines of Code.
	 * @callsequence int = sourceCodeName.returnOriginalCodeLength();, where
	 *               sourceCodeName is the name the user gives to the SourceCode
	 *               object.
	 * @inparam none
	 * @outparam int that equals the number of lines of code the original source
	 *           code had.
	 * @errorstested tested through use with main
	 * @errorsgenerated no known errors

	 * @createdate 10/12/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 * 
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
	 */
	public static final int returnOriginalCodeLength() {
		int originalOutput = originalCodeLength;
		return originalOutput;
	}

	/**
	 * @modulename returnOriginalSourceCode
	 * @description Returns the original (un-altered) source code in one big
	 *              string.
	 * @callsequence String = sourceCodeName.returnOriginalSourceCode();, where
	 *               sourceCodeName is the name the user gives to the SourceCode
	 *               object.
	 * @inparam none
	 * @outparam String that is the entire source code as one big string
	 * @errorstested tested through use with main
	 * @errorsgenerated no known errors

	 * @createdate 10/12/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 * 
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
	 */
	public static final String returnOriginalSourceCode() {
		String originalOutput = copyOfOriginal;
		return originalOutput;
	}

	/**
	 * @modulename resetCurrentLine
	 * @description Resets the Line that the SourceCode Object is working on.
	 * @callsequence String = sourceCodeName.returnOriginalSourceCode();, where
	 *               sourceCodeName is the name the user gives to the SourceCode
	 *               object.
	 * @inparam none
	 * @outparam none
	 * @errorstested tested through use with main
	 * @errorsgenerated no known errors

	 * @createdate 10/12/09
	 * @modlog <p>
	 *         <ul>
	 *         Who/When/Why
	 * 
	 *         Coding Standards Met: "Signed Code Leader(Jaron)"
	 *         <p>
	 *         Testing Standards Met: "Signed Test Leader(Jamie)"
	 */
	public static final void resetCurrentLine() {
		currentLine = 0;
	}
}
